﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace IconEditor.IconElements
{
    public partial class MoEllipse : BasicShape
    {
        #region Modelica annotation
        private bool visible = true;
        private Color lineColor = Color.FromArgb(0, 0, 0, 0);
        private Color fillColor = Color.FromArgb(0, 0, 0, 0);
        private LinePattern pattern = LinePattern.Solid;  // not yet implemented
        private FillPattern fillPattern = FillPattern.None;  // not yet implemented
        private double lineThickness = 0.25;
        private double[,] extent = new double[2, 2];  // not yet implemented
        #endregion

        public MoEllipse()
            : base()
        {
            InitializeComponent();

            HideControls();
        }

        public MoEllipse(bool _visible,
            Color _lineColor, Color _fillColor,
            LinePattern _pattern, FillPattern _fillPattern,
            double _lineThickness, double[,] _extent)
            : this()
        {
            visible = _visible;
            lineColor = _lineColor;
            fillColor = _fillColor;
            pattern = _pattern;  // not yet implemented
            fillPattern = _fillPattern;  // not yet implemented
            lineThickness = _lineThickness;
            extent = _extent;  // not yet implemented

            el.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
            el.Fill = new SolidColorBrush(fillColor);
            el.Stroke = new SolidColorBrush(lineColor);
            el.StrokeThickness = lineThickness;
            //{{x1, y1}, {x2, y2}}";

            this.Width = 2 * Math.Abs(extent[1, 0] - extent[0, 0]);
            this.Height = 2 * Math.Abs(extent[1, 1] - extent[0, 1]);
            Canvas.SetLeft(this, extent[0, 0] * 2 + 200);
            Canvas.SetTop(this, extent[0, 1] * 2 + 200);
        }

        protected override void ShowControls()
        {
            LeftTopBorder.Opacity = 0.4;
            RightTopBorder.Opacity = 0.4;
            LeftBottomBorder.Opacity = 0.4;
            RightBottomBorder.Opacity = 0.4;
            LeftBorder.Opacity = 0.4;
            RightBorder.Opacity = 0.4;
            TopBorder.Opacity = 0.4;
            BottomBorder.Opacity = 0.4;
        }

        protected override void HideControls()
        {
            LeftTopBorder.Opacity = 0.0;
            RightTopBorder.Opacity = 0.0;
            LeftBottomBorder.Opacity = 0.0;
            RightBottomBorder.Opacity = 0.0;
            LeftBorder.Opacity = 0.0;
            RightBorder.Opacity = 0.0;
            TopBorder.Opacity = 0.0;
            BottomBorder.Opacity = 0.0;
        }

        internal new void BasicShape_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseCaptured)
            {
                base.BasicShape_MouseMove(this, e);
            }
        }

        internal new void BasicShape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            base.BasicShape_MouseLeftButtonDown(sender, e);
        }

        internal new void BasicShape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            base.BasicShape_MouseLeftButtonUp(sender, e);
        }

        internal new void Resize_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseCaptured)
            {
                base.Resize_MouseMove(sender, e);
            }
        }

        internal override string GetAnnotation()
        {
            //Ellipse(visible=true, fillColor={255,127,127}, fillPattern=FillPattern.Solid, extent={{-35.55,-18.83},{64.45,22}}),
            String s = "Ellipse(visible=true, fillColor={255,127,127}, fillPattern=FillPattern.Solid, extent={{";

            //{{x1, y1}, {x2, y2}}";
            extent[0, 0] = (Canvas.GetLeft(this) - 200) / 2;
            extent[0, 1] = (Canvas.GetTop(this) - 200) / 2;
            extent[1, 0] = extent[0, 0] + this.Width / 2;
            extent[1, 1] = extent[0, 1] + this.Height / 2;

            s += Math.Round(extent[0, 0], 2).ToString();
            s += ",";
            s += Math.Round(extent[0, 1], 2).ToString();
            s += "},{";

            s += Math.Round(extent[1, 0], 2).ToString();
            s += ",";
            s += Math.Round(extent[1, 1], 2).ToString();
            s += "}}),";

            return s;
        }
    }
}
